<?php


class Apiaddress extends ApiBase {

	private $user_id;
	public function __construct(){
		parent::__construct();
		$this->user_id=$this->GetUser()['user_id'];
	}

	public function index(){
      $where['user_id']=$this->user_id;
	  $data=$this->db->select('address_id,name,phone,province,city,region,detail,default')->where($where)->get('address')->result_array();
	  $this->show($data);
	}


	public function add(){
      $post=$this->input->post();
      $post['create_time']=time();
      $post['update_time']=time();
      $post['user_id']=$this->user_id;
      unset($post['token']);
      
      if($post['default']==10){
      	$where['user_id']=$this->user_id;
      	$this->db->where($where)->update('address',array('default'=>20));
      }
      
      $res=$this->db->insert('address',$post);
      if ($res){
      	$this->show(array('code'=>1,'msg'=>'添加成功'));
	  }else{
      	$this->show(array('code'=>0,'msg'=>'添加失败'));
	  }
	}

	public function edit(){
      if ($this->post()){
		  $post=$this->input->post();
		  $post['create_time']=time();
		  $post['update_time']=time();
		  unset($post['token']);
		  
		  if($post['default']==10){
		  	$map2['user_id']=$this->user_id;
		  	$this->db->where($map2)->update('address',array('default'=>20));
		  }
		 
		  $res=$this->db->update('address',$post,array('address_id'=>$post['address_id']));
		  if ($res){
			  $this->show(array('code'=>1,'msg'=>'更新成功'));
		  }else{
			  $this->show(array('code'=>0,'msg'=>'更新失败'));
		  }
	  }
      $where['address_id']=(int)$this->uri->segment(4);
	  $data=$this->db->select('address_id,name,phone,province,city,region,detail,default')->where($where)->get('address')->row_array();
	  $this->show($data);
	}
     
     
     public function defaults(){
     	  $post=$this->input->post();
     	  $where['user_id']=$this->user_id;
     	  $this->db->where($where)->update('address',array('default'=>20));
     	  $data['default']=10;
     	  $data['update_time']=time();
     	  $res=$this->db->update('address',$data,array('address_id'=>$post['address_id']));
		  if ($res){
			  $this->show(array('code'=>1,'msg'=>'更新成功'));
		  }else{
			  $this->show(array('code'=>0,'msg'=>'更新失败'));
		  }
     	  
     }
     
     
	public function del(){
		$post=$this->input->post();
		$where['user_id']=$this->user_id;
		$where['address_id']=$post['address_id'];
		$info=$this->db->where($where)->get('address')->row_array();
		
		
		$map['user_id']=$this->user_id;
		$get=$this->db->where($map)->get('address')->row_array();
		$count=count($this->db->where($map)->get('address')->result_array());
		if($info['default']==10){
			if($get){
			$map2['address_id']=$get['address_id'];
        	$this->db->where($map2)->update('address',array('default'=>10));
			}
		}
		
		if($count==2){
			$this->db->where($map)->update('address',array('default'=>10));
		}
		
		$this->db->where($where)->delete('address');
		$res=$this->db->affected_rows();
		
		if ($res){
			$this->show(array('code'=>1,'msg'=>'删除成功'));
		}else{
			$this->show(array('code'=>0,'msg'=>'删除失败'));
		}
	}

}
